# -*- coding: utf-8 -*-
from lib.handlers import BaseWebHandler
from lib import utils
import webapp2


class MainWebHandler(BaseWebHandler):
    """Catch all handler for web interface"""
    def get(self, path):
        context = {}
        if 'XSRF-TOKEN' not in self.session:
            token = utils.random_string(size=12)
            self.session['XSRF-TOKEN'] = token
            self.response.set_cookie('XSRF-TOKEN', token)
            self.session_store.save_sessions(self.response)

        def do_bootstrap():
            """Check from config if the app is need to be bootstrapped"""
            config = webapp2.get_app().config
            if config.get('create_bootstrap'):
                context.update({
                    'doing_bootstrap': True
                })

        if not self.settings:
            do_bootstrap()
        else:
            app_settings = self.settings
            if not app_settings.bootstrapped and not app_settings.bootstrap_started:
                do_bootstrap()
            elif not app_settings.bootstrapped and app_settings.bootstrap_started:
                context.update({
                    'doing_bootstrap': True
                })

        self.render_response('all.html', **context)
